﻿using log4net;
using Restful;
using server.DAO;
using server.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using System.Threading.Tasks;

namespace server.Restful
{
    ////多线程
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class ServiceLifeImp : ILifeService
    {
        ILog Logger = log4net.LogManager.GetLogger("server.Restful.ServiceLifeImp");

        private LifeDao lifeDao = new LifeDao();
        private ScoreDao scoreDao = new ScoreDao();
        private GrounpLifeDao grounpLifeDao = new GrounpLifeDao();
        private UserDao userDao = new UserDao();
        /// <summary>
        /// 把自己的生命值和弹量加给别人
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="addLifeValue"></param>
        /// <param name="addbulletCount"></param>
        /// <param name="currentUser"></param>
        /// <returns></returns>
        public string TransferPlayerLifeBulletCount(string userId, 
            string addLifeValue, string addBulletCount, string currentUser)
        {
            return lifeDao.TransferPlayerLifeBulletCount(userId, addLifeValue, addBulletCount, currentUser);
        }
        /// <summary>
        /// 获取单个玩家的命值数值
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string GetPlayerLifeInfoByUser(string userId)
        {
           // Logger.Debug("userid:=" + userId);
            return lifeDao.GetPlayerInfoByUser(userId);
        }
        /// <summary>
        /// 通过玩家获取整个房间的命值数据
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public string GetRoomLifeInfoByUser(string userId)
        {
            return lifeDao.GetRoomLifeInfoByUser(userId);

        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="currrent"></param>
        /// <param name="currrentUser">0为玩家排行，1为room排行</param>
        /// <returns></returns>
        public string SearchFightScore(string userId, string userType, string currrentUser)
        {
            //if (currrentUser.Equals("0"))
            //{
                return scoreDao.SearchScore(int.Parse(userId), userType, true);
            //}
            //else
            //{
            //    return scoreDao.SearchScore(int.Parse(userId), userType, false);
            //}
        }

        public string SearchLifeByGrounp(string grounpId)
        {
            return  grounpLifeDao.SearchLifeByGrounp(grounpId);
        }

        public string UpdateLifeByGrounp(string grounpId, string lifeValue, string bulletCount,string hitReduceLifeValue)
        {
            return grounpLifeDao.UpdateLifeByGrounp( grounpId,  lifeValue,  bulletCount, hitReduceLifeValue);
        }

      //  public string SetGrounpLifeToUserList(string grounpId)
        //{
       //    // return grounpLifeDao.SetGrounpLifeToUserList(grounpId);
       // }

        /// <summary>
        /// 减命
        /// </summary>
        private readonly int SubTractValue = 1;
        public string SubTractLife(string userId)
        {
            lifeDao.SetLifeValue(userId, SubTractValue);
            return "";
        }

        /// <summary>
        /// 设置加命
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        //public string SetLife(string json)
        //{
        //    string[] strs = json.Split('|');
        //    string addLifeValue = strs[0];
        //    string bulletCountValue = strs[1];
        //    string userId = strs[2];
        //    userDao.UpdateLifeValue(int.Parse(addLifeValue), int.Parse(bulletCountValue), userId);
        //    return "";
        //}

        public void  UpdatePlayerDataBlueTooth(string userId, 
            string userName,string lifeValue, string bulletCount, string fightScore, string notificationMesage)
        {
            lifeDao.UpdatePlayerDataBlueTooth(userId, userName,lifeValue, bulletCount, fightScore, notificationMesage);
            // throw new NotImplementedException();
        }


        public string GetScoreByUser(string userId)
        {
            return scoreDao.GetScoreByUser(userId);
        }



        public string SearchBlueToothState(string userID)
        {
            return  lifeDao.SearchBlueToothState(userID);
        }

        public string GetGoldCoinList(string roomId)
        {
            return  lifeDao.GetGoldCoinList(roomId);
        }

        public string PayGoldCoin(string userId, string count)
        {
            return lifeDao.PayGoldCoin(userId, count);
        }
    }
}
